package com.gimi.cloud.bbp.manage.service.order.impl;

import java.util.*;

import com.gimi.cloud.bbp.manage.service.order.OrderInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.MyBeanUtils;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.dao.dao.order.OrderInvoiceDao;
import com.gimi.cloud.bbp.dto.order.OrderInvoiceDTO;
import com.gimi.cloud.bbp.entity.order.OrderInvoiceEntity;

import tk.mybatis.mapper.entity.Example;
@Service
@Transactional
public class OrderInvoiceServiceImpl implements OrderInvoiceService {
	
	@Autowired
	private OrderInvoiceDao orderInvoiceDao;

	@Override
	public List<OrderInvoiceEntity> orderInvoiceByCondition(OrderInvoiceDTO DTO) throws Exception {
        Example example = new Example(OrderInvoiceEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("is_delete =", "0");
        //订单id
        if(StringUtil.isBlank(DTO.getoId())){
        	return null;
        }
        criteria.andCondition("o_id = ", DTO.getoId());
        List<OrderInvoiceEntity> list = orderInvoiceDao.selectByExample(example);
        return list;
	}

	@Override
	public void deleteOrderInvoice(OrderInvoiceDTO DTO) throws Exception {
		OrderInvoiceEntity entity = orderInvoiceDao.selectByPrimaryKey(DTO.getOiId());
        if (entity != null) {
            //删除订单发票信息
            entity.setIsDelete(1);//设置删除属性
            int i = orderInvoiceDao.updateByPrimaryKey(entity);
            if (i <= 0 ) {
				throw new MsgException(ResponseCodeEnum.FAILURE, "订单发票信息删除失败!");
			}
        }
	}

	@Override
	public OrderInvoiceDTO getSingleOrderInvoice(OrderInvoiceDTO DTO)throws Exception {
		OrderInvoiceEntity entity = orderInvoiceDao.selectByPrimaryKey(DTO.getOiId());
		OrderInvoiceDTO dto = new OrderInvoiceDTO();
        MyBeanUtils.copyProperties(entity,dto);
        return dto;
	}

	@Override
	public void updateOrderInvoice(OrderInvoiceDTO DTO) throws Exception {
		OrderInvoiceEntity entity = orderInvoiceDao.selectByPrimaryKey(DTO.getOiId());
		MyBeanUtils.copyPropertiesIgnoreNull(DTO,entity);
        entity.setUpdateTime(new Date());
        int i = orderInvoiceDao.updateByPrimaryKey(entity);
        if (i <= 0 ) {
        	throw new MsgException(ResponseCodeEnum.FAILURE, "更新订单发票信息失败!");
        }
	}
	
	
	
	
	
	
	
	
	

}
